/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor
 */
package com.esoft;

import com.noelios.restlet.ext.servlet.ServletConverter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.restlet.Router;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author odpsoft
 */
public class RestletSpringServlet extends HttpServlet {

    private static final long serialVersionUID = 3401944819646147256L;
    private ServletConverter converter;
    private RestletMapperBean mapper;

    @Override
    public void init() throws ServletException {
        super.init();

        // get restlet mapper from spring including all initialized restlets  
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        mapper = (RestletMapperBean) applicationContext.getBean("restMapper");

        // servlet api integration class  
        converter = new ServletConverter(getServletContext());

    }

    @Override
    public void service(HttpServletRequest pRequest, HttpServletResponse pResponse)
            throws ServletException, IOException {

        // create router that will dispatch request to restlet based on matching path  
        Router router = new Router();

        // attach restlets to router by  
        //router.attach("/users/{username}",HelloWorldResource.class);        
        mapper.init(router);


        // finally service request through servlet converter  
        converter.setTarget(router);
        converter.service(pRequest, pResponse);

    }
}  